package cn.iocoder.yudao.module.mqtt.service.devicealermrecord;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.devicealermrecord.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicealermrecord.DeviceAlermRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.devicealermrecord.DeviceAlermRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * 报警记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DeviceAlermRecordServiceImpl implements DeviceAlermRecordService {

    @Resource
    private DeviceAlermRecordMapper deviceAlermRecordMapper;

    @Override
    public Long createDeviceAlermRecord(DeviceAlermRecordSaveReqVO createReqVO) {
        // 插入
        DeviceAlermRecordDO deviceAlermRecord = BeanUtils.toBean(createReqVO, DeviceAlermRecordDO.class);
        deviceAlermRecordMapper.insert(deviceAlermRecord);
        // 返回
        return deviceAlermRecord.getId();
    }

    @Override
    public void updateDeviceAlermRecord(DeviceAlermRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceAlermRecordExists(updateReqVO.getId());
        // 更新
        DeviceAlermRecordDO updateObj = BeanUtils.toBean(updateReqVO, DeviceAlermRecordDO.class);
        deviceAlermRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceAlermRecord(Long id) {
        // 校验存在
        validateDeviceAlermRecordExists(id);
        // 删除
        deviceAlermRecordMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceAlermRecordListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceAlermRecordExists(ids);
        // 删除
        deviceAlermRecordMapper.deleteByIds(ids);
        }

    private void validateDeviceAlermRecordExists(List<Long> ids) {
        List<DeviceAlermRecordDO> list = deviceAlermRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_ALERM_RECORD_NOT_EXISTS);
        }
    }

    private void validateDeviceAlermRecordExists(Long id) {
        if (deviceAlermRecordMapper.selectById(id) == null) {
            throw exception(DEVICE_ALERM_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public DeviceAlermRecordDO getDeviceAlermRecord(Long id) {
        return deviceAlermRecordMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceAlermRecordDO> getDeviceAlermRecordPage(DeviceAlermRecordPageReqVO pageReqVO) {
        return deviceAlermRecordMapper.selectPage(pageReqVO);
    }

}